package monopoly;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import monopoly.ui.PlayerSelector;

public class GameStart {
	
	/**
	 * Main Game Frame
	 */
	JFrame frame;
	
	/**
	 * Buttons for the Main Screen
	 */
	JButton btnNewGame;
	JButton btnQuit;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GameStart game = new GameStart();
		game.start();
	}

	public void start() {
		// Start Drawing the Main Screen 
		frame = new JFrame("Monopoly");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setSize(400, 250);
		frame.setVisible(true);

		//Place for Buttons
		JPanel btnPanel = new JPanel();
		btnPanel.setBackground(Color.WHITE);
		btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
		
		//Place for images
		BackgroundImgPanel imgPanel = new BackgroundImgPanel();
		imgPanel.setBackground(Color.WHITE);

		btnNewGame = new JButton("New Game");
		btnQuit = new JButton("Quit");
		
		//Making Buttons appear on the Center of the Screen
		btnNewGame.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnQuit.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		//Adding Action Listener for Quit
		btnNewGame.addActionListener(new NewGameBtnListener());
		btnQuit.addActionListener(new QuitBtnListener());

		//Adding Button to the Panel
		btnPanel.add(btnNewGame);
		btnPanel.add(btnQuit);
		
		//Adding the Panel to the Frame
		frame.getContentPane().add(BorderLayout.CENTER, imgPanel);
		frame.getContentPane().add(BorderLayout.SOUTH, btnPanel);
	}

	class NewGameBtnListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// Make current window invisible.
			//frame.setVisible(false);
			
			// Player Selector
			PlayerSelector selector = new PlayerSelector(frame);
			selector.init();
			
			// Creating a new Game GUI
			//MonopolyGUI gui = new MonopolyGUI();
			
			// Starting the Game
			//gui.init();

		}
	}

	class QuitBtnListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);

		}
		
	}
	
	class BackgroundImgPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		
		BufferedImage image;
		
		public BackgroundImgPanel(){
			try {
				image = javax.imageio.ImageIO.read(new File("images/logo.gif"));
			} catch (IOException e) {
				throw (new RuntimeException(e));
			}
		}
		
		protected void paintComponent(Graphics g) {
	        super.paintComponent(g);
	        g.drawImage(image, 30, 30, this);
	    }

	}

}
